package com.teapos.web;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.teapos.service.ShiftService;
import com.teapos.util.Maps;
import com.teapos.util.Tool;

@SuppressWarnings({"rawtypes", "unchecked"})
@RequestMapping("/shift")
@Controller
public class ShiftController extends BaseController {
	
	@Autowired
	private ShiftService shiftService;
	
	@RequestMapping("/summary")
	public String handAccount(HttpServletRequest req) {
		int storeId = Tool.toInt(super.getUser(req).get("stid"));
		req.setAttribute("summary", shiftService.summaryShiftInfo(storeId));
		return "shift/summary";
	}
	
	@ResponseBody
	@RequestMapping("/save")
	public String save(HttpServletRequest req) {
		Map param = super.getParaMap(req);
		param.put("user", super.getUser(req));
		try {
			shiftService.saveInfo(param);
			return success("提交成功！！");
		} catch (Exception e) {
			e.printStackTrace();
			return fail("提交失败！！");
		}
	}
	
	@RequestMapping("/check_view")
	public String check_view(HttpServletRequest req) {
		req.setAttribute("info", shiftService.findLastRecord(Tool.toInt(super.getUser(req).get("store_id"))));
		return "shift/check";
	}
	
	@ResponseBody
	@RequestMapping("/check")
	public String check(HttpServletRequest req) {
		Map user = super.getUser(req);
		int userId = Tool.toInt(user.get("user_id"));
		String userName = Tool.toString(user.get("user_name"));
		Map map = new HashMap();
		map.put("id", req.getParameter("id"));
		map.put("user_id", userId);
		map.put("user_name", userName);
		try {
			shiftService.updateInfo(map);
			return success("核对成功！！");
		} catch (Exception e) {
			e.printStackTrace();
			return fail("核对失败！！");
		}
	}
	
	@RequestMapping("/list")
	public String list(Integer pageNo, Map map) {
		int no = Tool.initPageNo(pageNo);
		map.put("list", shiftService.queryByPage(no));
		map.put("page", Maps.newHashMap("pageNo", no, "total", shiftService.queryCount(), "js", "turnpage"));
		return "shift/list";
	}
	
	@RequestMapping("/detail")
	public String detail(String id, Map map) {
		map.put("info", shiftService.findShiftById(id));
		return "shift/detail";
	}
	
	@RequestMapping(value="/takeStock", method=RequestMethod.GET)
	public String takeStock() {
		return "shift/takeStock";
	}
	
	@ResponseBody
	@RequestMapping(value="/takeStock", method=RequestMethod.POST)
	public String postTakeStock(HttpServletRequest req) {
		Map user = super.getUser(req);
		try {
			shiftService.takeStock(Maps.newHashMap("codes", req.getParameter("codes"), "shelfCode", req.getParameter("shelfCode")
					, "userId", user.get("user_id"), "userName", user.get("user_name"), "storeId", user.get("store_id")
					, "storeName", user.get("store_name")));
			return success("保存成功！！");
		} catch (Exception e) {
			e.printStackTrace();
			return success("保存失败！！");
		}
	}
	
	@RequestMapping("/scan")
	public String scan(String qrcode, String shelfCode, ModelMap model) {
		model.addAttribute("qrcode", shiftService.findTdcodeByCode(qrcode, shelfCode));
		return "shift/qrcode";
	}
	
	@RequestMapping("/takeStockList")
	public String takeStockList(HttpServletRequest req) {
		Map user = super.getUser(req);
		int storeId = Tool.toInt(user.get("store_id"));
		

		Map param = super.getParaMap(req);
		//System.err.println(param);
		param.put("storeId", storeId);
		req.setAttribute("takeStocks", shiftService.getTakeStocksByPage(param));
		req.setAttribute("page", Maps.newHashMap("pageNo", Tool.initPageNo(req.getParameter("pageNo")), "total",
				shiftService.getTakeStockCount(param), "js", "turnpage"));
		
		return "shift/takeStockList";
	}
	
	@RequestMapping("/takeStockdiff")
	public String takeStockdiff(HttpServletRequest req) {
		Map user = super.getUser(req);
		int storeId = Tool.toInt(user.get("store_id"));

		Map param = super.getParaMap(req);
		//System.err.println(param);
		param.put("storeId", storeId);
		req.setAttribute("diffs", shiftService.getDiffDetailsByPage(param));
		req.setAttribute("page", Maps.newHashMap("pageNo", Tool.initPageNo(req.getParameter("pageNo")), "total",
				shiftService.getDiffDetailsCount(param), "js", "turnpage"));
		return "shift/takeStockdiff";
	}
	
	@ResponseBody
	@RequestMapping("/diffConfirm")
	public String diffConfirm(HttpServletRequest req) {
		Map user = super.getUser(req);
		Map param = Maps.newHashMap("id", req.getParameter("id"), "userId", user.get("user_id")
				, "userName", user.get("user_name"));
		try {
			shiftService.confirm(param);
			return success("确认成功！！");
		} catch (Exception e) {
			e.printStackTrace();
			return fail("确认失败！！");
		}
	}

}
